package com.ushine.web.config.duird;

import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * @Author: yls
 * @Date: 2022/02/22 18:26
 * @Description: 日志库的数据源配置
 *              将配置类注入到bean容器，使ConfigurationProperties注解类生效
 * @Version 1.0
 */
@Configuration
@Slf4j
@EnableConfigurationProperties({DataSourceProperties.class, DataSourceCommonProperties.class})
public class LogDruidDataSourceConfig {
    @Resource
    private DataSourceProperties dataSourceProperties;

    @Resource
    private DataSourceCommonProperties dataSourceCommonProperties;

    /**
     * 功能描述:
     * 〈新建bean实例  〉
     *
     * @return : javax.sql.DataSource
     * @author : yls
     * @date : 2022/3/11 16:04
     */
    @Bean("logDruidDataSource")
    @Qualifier("logDruidDataSource")
    public DataSource logDruidDataSource() {
        DruidDataSource datasource = new DruidDataSource();
        //配置数据源属性
        datasource.setUrl(dataSourceProperties.getSecondary().get("url"));
        datasource.setUsername(dataSourceProperties.getSecondary().get("username"));
        datasource.setPassword(dataSourceProperties.getSecondary().get("password"));
        datasource.setDbType(dataSourceProperties.getSecondary().get("db-type"));
        datasource.setDriverClassName(dataSourceProperties.getSecondary().get("driver-class-name"));
        // 统一配置项
        dataSourceCommonProperties.getCommonProperties(datasource);
        return datasource;
    }


}